import * as request from '../constant/fetch-request'
import HTTP_CODE from '../constant/http-code'

export const refreshDiffProgram = data => ({
  type: 'REFRESH_DIFF_PROGRAM',
  data
})
export const refreshDiffProgramHistory = data => ({
  type: 'REFRESH_DIFF_PROGRAM_HISTORY',
  data
})
export const refreshDiffTaskHistory = data => ({
  type: 'REFRESH_DIFF_TASK_HISTORY',
  data
})

export const getDiffPrograms = (programId, callback) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/diff/programs/${programId}`)
      if (res.status === HTTP_CODE.OK) {
        callback(res.body)
        dispatch(refreshDiffProgram(res.body))
      }
    })()
  }
}
export const getDiffProgramHistory = (programId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/diff/programs/${programId}/history`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshDiffProgramHistory(res.body))
      }
    })()
  }
}
export const getDiffTaskHistory = (taskId) => {
  return dispatch => {
    (async () => {
      const res = await request.get(`./api/diff/tasks/${taskId}/history`)
      if (res.status === HTTP_CODE.OK) {
        dispatch(refreshDiffTaskHistory(res.body))
      }
    })()
  }
}
